package cn.topevery.garden.dal.logic;

import cn.topevery.garden.dal.dao.ParkCompanyDao;
import cn.topevery.garden.entity.po.Company;
import cn.topevery.garden.entity.query.CompanyQuery;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.basis.dal.logic.SysDeptLogic;
import hw.topevery.basis.dal.logic.SysUserInDeptLogic;
import hw.topevery.basis.dal.logic.SysUserLogic;
import hw.topevery.basis.entity.fetch.UserFetch;
import hw.topevery.basis.entity.po.SysDept;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author code-generate
 * @date 2021-08-06
 */
@Service
public class ParkCompanyLogic extends BaseLogic<Company> {
    @Autowired
    ParkCompanyDao companyDao;

    @Autowired
    SysDeptLogic sysDeptLogic;

    @Autowired
    SysUserInDeptLogic sysUserInDeptLogic;

    @Autowired
    SysUserLogic sysUserLogic;

    @Override
    public BaseEntityDao<Company, Integer> getDao() {
        return companyDao;
    }


    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<Company> listPage(CompanyQuery query) {
        return companyDao.listPage(query);
    }

    public boolean saveOrUpdate(String userId, Company entity) {
        companyDao.saveOrUpdate(userId, entity);
        SysDept dept = new SysDept();
        if (entity.getId() == null || entity.getId().equals(0)) {
            // 新增
            dept.deptId = entity.getCompanyId();
        } else {
            // 修改
            dept = sysDeptLogic.getEntityByDataId(entity.getCompanyId());
        }
        dept.deptAcceptevt = 1;
        dept.deptCode = "";
        dept.deptContracts = entity.getContacts();
        dept.deptContractsNum = entity.getContactsPhone();
        dept.deptName = entity.getName();
        dept.deptPid = "189FB67C-703A-4EDF-9200-10B3200E4022";
        dept.deptShortName = entity.getNameShort();
        dept.deptStatus = 10101;
        dept.deptType = 15003;
        dept.mapId = "f41889ad-585b-413d-878e-7040991f468d";
        dept.mapType = 13002;
        dept.remark = entity.descs;
        dept.dbStatus = false;
        List<SysDept> depts = new ArrayList<>();
        depts.add(dept);
        sysDeptLogic.saveOrUpdate(userId, depts);
        return true;
    }

    /**
     * 根据自增id批量删除
     *
     * @param ids
     */
    public void deleteBatchById(String userId, Integer[] ids) {
        companyDao.delete(userId, ids);
    }

    /**
     * 描述 检查是否已经存在.
     *
     * @param fieldName 字段名
     * @param value     字段值
     * @return the boolean （true已存在，false不存在）
     * @author wenzhao.lin
     * @created 2021 -08-19 09:23:47 Is exit boolean.
     */
    public boolean isExit(String fieldName, String value) {
        return companyDao.isExit(fieldName, value);
    }

    //按条件查询所有企业
    public List<Map<String, Object>> getShortList(CompanyQuery query) {
        return companyDao.getShortList(query);
    }

    //查询企业下的所有用户
    public JsonResult<?> getUserInCompany(Integer companyId) {
        Company company = companyDao.getEntity(companyId);
        if (company == null) {
            return JsonResult.error("查询企业不存在！");
        }
        UserFetch userFetch = new UserFetch();
        userFetch.setDeptId(company.companyId);
        List<SysUser> userList = sysUserLogic.getList(userFetch);
        return JsonResult.ok(userList);
    }
}
